<template>
  <div class="subjects-container">
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="18">
          <el-form label-width="80px" :inline="true" size="small">
            <el-form-item label="学科名称">
              <el-input
                v-model="queryInfo.subjectName"
                @keyup.enter.native="search"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="clean">清除</el-button>
              <el-button type="primary" size="small" @click="search"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align: right">
          <el-button
            icon="el-icon-edit"
            size="small"
            type="success"
            @click="openDialog"
            >新增学科</el-button
          >
        </el-col>
      </el-row>

      <!-- 提示框组件 -->
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        :closable="false"
        style="margin-bottom: 15px"
      >
      </el-alert>

      <!-- table组件 -->
      <el-table :data="subjectList" style="width: 100%">
        <el-table-column label="序号" type="index" width="48px">
        </el-table-column>
        <el-table-column label="学科名称" width="width" prop="subjectName">
        </el-table-column>
        <el-table-column label="创建者" width="width" prop="username">
        </el-table-column>
        <el-table-column label="创建日期" width="160px">
          <template #default="{ row }">
            {{ formatTime(row.addDate) }}
          </template>
        </el-table-column>
        <el-table-column label="前台是否显示" width="width">
          <template #default="{ row }">
            {{ row.isFrontDisplay === 1 ? "是" : "否" }}
          </template>
        </el-table-column>
        <el-table-column
          label="二级目录"
          width="width"
          prop="twoLevelDirectory"
        >
        </el-table-column>
        <el-table-column label="标签" width="width" prop="tags">
        </el-table-column>
        <el-table-column label="题目数量" width="width" prop="totals">
        </el-table-column>
        <el-table-column label="操作" width="240px">
          <template #default="{ row }">
            <el-button
              type="text"
              @click="
                $router.push(
                  `/subjects/directorys?id=${row.id}&name=${encodeURIComponent(
                    row.subjectName
                  )}`
                )
              "
              >学科分类</el-button
            >
            <el-button
              type="text"
              @click="
                $router.push(
                  `/subjects/tags?id=${row.id}&name=${encodeURIComponent(
                    row.subjectName
                  )}`
                )
              "
              >学科标签</el-button
            >
            <el-button type="text" @click="editDialog(row)">修改</el-button>
            <el-button type="text" @click="delSubject(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="text-align: center; margin-top: 20px"
      >
      </el-pagination>
    </el-card>

    <!-- 对话框组件 -->
    <SubjectAdd ref="dialog" :showDialog.sync="showDialog"></SubjectAdd>
  </div>
</template>

<script>
// 导入获取学科列表和删除学科的方法
import { list as getSubjectsListApi, remove as delSubjectApi } from '@/api/hmmm/subjects.js'
// 导入dayjs进行事件格式化
import dayjs from 'dayjs'
// 导入对话框组件
import SubjectAdd from '../components/subjects-add.vue'
export default {
  components: {
    SubjectAdd
  },
  data () {
    return {
      // 分页组件的total
      total: 0,
      // 学科列表
      subjectList: [],
      // 查询参数
      queryInfo: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      // 控制对话框的显示与隐藏
      showDialog: false
    }
  },
  created () {
    this.getSubjectsList()
  },
  methods: {
    // 获取学科列表
    async getSubjectsList () {
      const { data } = await getSubjectsListApi(this.queryInfo)
      console.log(data)
      this.subjectList = data.items
      this.total = data.counts
    },
    // 监听page的改变
    handleCurrentChange (newPage) {
      this.queryInfo.page = newPage
      this.getSubjectsList()
    },
    // 监听pagesize的改变
    handleSizeChange (newPagesize) {
      this.queryInfo.pagesize = newPagesize
      this.getSubjectsList()
    },
    // 搜索功能
    search () {
      this.getSubjectsList()
    },
    // 时间格式化
    formatTime (time, str = 'YYYY-MM-DD HH:mm:ss') {
      return dayjs(time).format(str)
    },
    // 打开添加学科对话框
    openDialog () {
      this.showDialog = true
    },
    // 删除学科
    async delSubject (row) {
      try {
        await this.$confirm(`确定删除${row.subjectName}学科吗?`)
        await delSubjectApi({ id: row.id })
        this.$message.success('删除成功')
        this.getSubjectsList()
      } catch (error) {
        console.log(error)
        this.$message.info('取消了删除')
      }
    },
    // 打开修改学科对话框
    editDialog (row) {
      this.showDialog = true
      // this.$refs.dialog.formModel = {
      //   id: row.id, subjectName: row.subjectName, isFrontDisplay: row.isFrontDisplay + ''
      // }
      this.$refs.dialog.getSubjectDetail(row.id)
    },
    // 清除功能
    clean () {
      this.queryInfo.subjectName = ''
      this.getSubjectsList()
    }
  }
}
</script>

<style scoped lang="scss">
.subjects-container {
  padding: 10px 10px;
}
</style>
